import 'dart:math';
import 'dart:ui';

import 'package:flutter/cupertino.dart';

class HBScreen {
  factory HBScreen() =>_getInstance();
  static HBScreen get instance => _getInstance();
  static HBScreen _instance;

  static HBScreen _getInstance() {
    if (_instance == null) {
      _instance = new HBScreen._internal();
    }
    return _instance;
  }

  HBScreen._internal() {}

  Size _designSize = Size(360, 690);
  double _screenW;
  double _screenH;
  double _scaleW;
  double _scaleH;
  double _scaleText;

  init({Size designSize}) {
    if (designSize != null) {
      _designSize = designSize;
    }
    final dpr = window.devicePixelRatio;
    _screenH = window.physicalSize.height/dpr;
    _screenW = window.physicalSize.width/dpr;
    _scaleH = _screenH/_designSize.height;
    _scaleW = _screenW/_designSize.width;
    _scaleText = min(_scaleH, _scaleW);
  }
}

extension HBScreenUtil on HBScreen {
  // 屏幕宽度
  double get screenWidth => HBScreen()._screenW;
  // 屏幕高度
  double get screenHeight => HBScreen()._screenH;
  // 适配后的高度
  double setHeight(num height) => height * _scaleH;
  // 适配后的宽度
  double setWidth(num width) => width * _scaleW;

  double setText(num fontSize) => fontSize * _scaleText;
}

extension HBScreenSize on num {
  double get w => HBScreen().setWidth(this);
  double get h => HBScreen().setHeight(this);
  double get sp => HBScreen().setText(this);
  double get sw => HBScreen().screenWidth * this;
  double get sh => HBScreen().screenHeight * this;
}